package com.agileboot.admin.controller.airport;

import com.agileboot.admin.customize.aop.accessLog.AccessLog;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.enums.common.BusinessTypeEnum;
import com.agileboot.common.utils.poi.CustomExcelUtil;
import com.agileboot.domain.common.command.BulkOperationCommand;
import com.agileboot.domain.airport.inspectionCheck.InspectionCheckApplicationService;
import com.agileboot.domain.airport.inspectionCheck.command.AddInspectionCheckCommand;
import com.agileboot.domain.airport.inspectionCheck.command.UpdateInspectionCheckCommand;
import com.agileboot.domain.airport.inspectionCheck.dto.InspectionCheckDTO;
import com.agileboot.domain.airport.inspectionCheck.query.InspectionCheckQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@Tag(name = "检查项API", description = "检查项相关的增删查改")
@RestController
@RequestMapping("/inspection/check")
@Validated
@RequiredArgsConstructor
public class InspectionCheckController extends BaseController {

    private final InspectionCheckApplicationService applicationService;

    @Operation(summary = "检查项列表")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<InspectionCheckDTO>> list(InspectionCheckQuery query) {
        return ResponseDTO.ok(applicationService.getInspectionCheckList(query));
    }

    @Operation(summary = "检查项列表导出")
    @AccessLog(title = "检查项管理", businessType = BusinessTypeEnum.EXPORT)
    @GetMapping("/excel")
    public void export(HttpServletResponse response, InspectionCheckQuery query) {
        List<InspectionCheckDTO> list = applicationService.getInspectionCheckListAll(query);
        CustomExcelUtil.writeToResponse(list, InspectionCheckDTO.class, response);
    }

    @Operation(summary = "检查项详情")
    @GetMapping("/{id}")
    public ResponseDTO<InspectionCheckDTO> getInfo(@PathVariable Long id) {
        return ResponseDTO.ok(applicationService.getInspectionCheckInfo(id));
    }

    /**
     * 开始检查
     */
    @Operation(summary = "开始检查")
    @AccessLog(title = "检查项管理", businessType = BusinessTypeEnum.MODIFY)
    @PostMapping("/startInspection")
    public ResponseDTO<Void> startInspection(@RequestBody @Valid UpdateInspectionCheckCommand command) {
        applicationService.startInspection(command);
        return ResponseDTO.ok();
    }

    @Operation(summary = "添加检查项")
    @AccessLog(title = "检查项管理", businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseDTO<Void> add(@RequestBody AddInspectionCheckCommand command) {
        applicationService.addInspectionCheck(command);
        return ResponseDTO.ok();
    }

    @Operation(summary = "修改检查项")
    @AccessLog(title = "检查项管理", businessType = BusinessTypeEnum.MODIFY)
    @PutMapping
    public ResponseDTO<Void> edit(@RequestBody UpdateInspectionCheckCommand command) {
        applicationService.updateInspectionCheck(command);
        return ResponseDTO.ok();
    }

    @Operation(summary = "删除检查项")
    @AccessLog(title = "检查项管理", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping
    public ResponseDTO<Void> remove(@RequestParam @NotNull @NotEmpty List<Long> ids) {
        applicationService.deleteInspectionCheck(new BulkOperationCommand<>(ids));
        return ResponseDTO.ok();
    }

}
